package com.xboard.server.manager.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.jdo.PersistenceManager;

import org.springframework.stereotype.Component;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Query;
import com.xboard.server.manager.DataManager;

@Component("dataManager")
public class DataManagerImpl implements DataManager{
	
	@Override
	public void insert(Entity enty){
        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        datastore.put(enty);
	}
	@Override
	public List<Map<String,Object>> list(Query query){
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
	    List<Entity> enList = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(10000));
	    List<Map<String,Object>> list= new ArrayList<Map<String,Object>>();
	    
	    for(Entity e : enList){
	    	list.add(e.getProperties());
	    }	    
	    return list;
	}
	@Override
	public Entity select(Query query){
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
	    Entity en = datastore.prepare(query).asSingleEntity();
	    return en;
	}	
	@Override
	public void update(Entity enty){
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		datastore.put(enty);
	}
	@Override
	public void delete(Entity enty){
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        datastore.delete(enty.getKey());
	}
	@Override
	public void deleteAll(Query query){
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
	    List<Entity> enList = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(1000));
	    for(Entity e : enList){
    		datastore.delete(e.getKey());
	    }
	}	
	@Override
	public void deleteChart(Query query){
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
	    List<Entity> enList = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(10000));
	    int i=0;
	    for(Entity e : enList){
	    	i++;
	    	//System.out.println(" alive : "+e.getProperty("date"));
	    	if(i>600){ // 최근 900개만 남기고 나머지 삭제
	    		datastore.delete(e.getKey());
	    		//System.out.println(i+ ": delete data : "+e.getProperty("date"));
	    	}
	    }
	    System.out.println("deleteChart ListCnt : "+enList.size());
	}		
	@Override
	public Key getKey(Query query){
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
	    Entity en = datastore.prepare(query).asSingleEntity();
        return en.getKey();
	}	
	@Override
	public Entity getEntity(Query query){
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
	    Entity en = datastore.prepare(query).asSingleEntity();
        return en;
	}
}
